_package("alz.mui");

_import("alz.core.TaskSchedule");
_import("alz.core.ActionManager");
_import("alz.mui.Component");
_import("alz.mui.LinkLabel");
_import("alz.mui.LinkButton");
_import("alz.mui.FormElement");

/**
 * 基础面板组件
 */
_class("Pane", Component, function(){
	this._init = function(){
		_super._init.call(this);
		this._actionManager = new ActionManager(this);
		this._app = null;
		this._currentSender = null;
		//this._taskSchedule = new TaskSchedule();  //任务调度器
		this._lastTime = 0;
		//this._lastAct = "";
	};
	this.bind = function(obj, app){
		if(app){
			this._app = app;
		}
		this.init(obj);
	};
	this.dispose = function(){
		/*
		if(this._taskSchedule){
			this._taskSchedule.dispose();
			this._taskSchedule = null;
		}
		*/
		this._currentSender = null;
		this._app = null;
		this._actionManager.each(function(i, node){
			switch(node.tagName){
			case "FORM":
				//node.onsubmit = null;
				node._ptr.dispose();
				//node._ptr = null;
				break;
			case "SELECT":
				node.onchange = null;
				break;
			case "BUTTON":
				node.onclick = null;
				break;
			case "INPUT":
				switch(node.type){
				case "button":
				case "submit":
				case "reset" :
					/*
					if(!this._app.isCNMail()){
						node.onclick = null;
					}
					*/
					if(node._ptr){
						node._ptr.dispose();
						//node._ptr = null;
					}
					break;
				case "checkbox":
					node.onclick = null;
					break;
				default:
					//continue;
					break;
				}
				break;
			case "A":
			case "H1":
			case "H2":
			default:  //LI
				if(node._ptr){
					if(!node._ptr._auto_dispose){
						node._ptr.dispose();  //{LinkLabel}
					}
				}
				break;
			}
		});
		this._actionManager.dispose();
		this._actionManager = null;
		_super.dispose.apply(this);
	};
	this.getApp = function(){
		return this._app;
	};
	this.setApp = function(v){
		this._app = v;
	};
	this.getActionEngine = function(){
		return this;
	};
	this.getCurrentSender = function(){
		return this._currentSender;
	};
	/**
	 * 通过模板名创建一组DOM元素
	 * @param {Element} parent 父元素
	 * @param {String} tpl 模板名
	 * @return {Element}
	 */
	this.createTplElement = function(parent, tpl, app){
		return this.createDomElement(parent, (app || this._app).getTpl(tpl)/*, ".module"*/);
	};
	/**
	 * 初始化组件中的动作元素，使这些元素可以响应默认事件触发其action
	 * [TODO]如何避免已经初始化过的元素重复初始化？
	 * @param {HTMLElement} element
	 * @param {Object} owner
	 * @param {Array} customTags 自定义支持action的标签
	 */
	this.initActionElements = function(element, owner, customTags){
		element = element || this._self;
		owner = owner || this.getActionEngine();
		function onAction(ev){
			//ev = ev || window.event;
			var ret;
			if(!this.disabled){
				ret = owner.doAction(this.getAttribute("_action"), this._sender || this);
			}else{
				ret = false;
			}
			ev.stopPropagation();
			return ret;
		}
		var tags = ["form", "select", "button", "input", "a", "h1"/*, "h2"*/];
		if(customTags){
			tags = tags.concat(customTags);
		}
		for(var i = 0, len = tags.length; i < len; i++){
			var tag = tags[i];
			var nodes = element.getElementsByTagName(tag);
			for(var j = 0, len1 = nodes.length; j < len1; j++){
				var node = nodes[j];
				//if(node.getAttribute("aui_aid")) continue;  //避免重复绑定action相关事件
				if(runtime._element.getUid(node)){
					if(node._ptr instanceof ActionElement){
						node._ptr.setActionManager(this._actionManager);
					}
					continue;
				}
				var act = node.getAttribute("_action");  //runtime._element.data(node, "action")
				if(!act) continue;  //若没有_action属性，就不处理
				switch(tag){
				case "form":
					//node.onsubmit = onAction;
					var btn = new FormElement();
					btn.setActionManager(this._actionManager);
					btn.bind(node);
					this._actionManager.add(node);
					btn = null;
					break;
				case "select":
					node.onchange = onAction;
					this._actionManager.add(node);
					break;
				case "button":
					node.onclick = onAction;
					this._actionManager.add(node);
					break;
				case "input":
					switch(node.type){
					case "button":
					case "submit":
					case "reset" :
						var btn = new LinkButton();
						btn.setActionManager(this._actionManager);
						btn.bind(node);
						//btn.onclick = onAction;
						this._actionManager.add(node);
						btn = null;
						break;
					case "checkbox":
						node.onclick = onAction;
						this._actionManager.add(node);
						break;
					default:
						continue;
					}
					break;
				case "a":
				case "h1":
				case "h2":
				default:  //其他自定义标签(li)和a行为一致
					if(node._ptr){  //如果已经创建对应的组件了
						if(node._ptr instanceof ActionElement){
							node._ptr.setActionManager(this._actionManager);
							node._ptr._auto_dispose = true;
						}else{
							runtime.error("[Pane::initActionElements]");
						}
					}else{
						var label = new LinkLabel();
						label.bind(node, this._actionManager);
						/*
						label.onAction = function(ev){
							return onAction.call(this._self, ev);
						};
						*/
					}
					this._actionManager.add(node);
					break;
				}
				node = null;
			}
			nodes = null;
		}
	};
	/**
	 * Action 工作模型
	 * [TODO]
	 * 1)级联激发的 Action 动作只把最初的动作压入动作栈，这样能够保证正确的动作回
	 *   退机制。
	 */
	this.doAction = function(act, sender){
		var time = new Date().getTime();
		if(time < this._lastTime + 500){
			this._lastTime = time;
			return false;
		}
		//if(time < this._lastTime + 800 && this._lastAct == act){
		//	return false;
		//}
		this._lastTime = time;
		//this._lastAct = act;
		this._currentSender = sender;
		var ret, key = "do_" + act;
		if(key in this && typeof this[key] == "function"){
			ret = this[key](act, sender);
		}else{  //自己处理不了的交给APP处理
			ret = this._app.doAction.apply(this._app, arguments);
			//runtime.error("[Pane::doAction]未定义的act=" + act);
			//ret = false;
		}
		this._currentSender = null;
		//[TODO]应该在动画播完之后运行，如何保证呢？
		/*
		runtime.startTimer(10, this, function(){
			this._taskSchedule.run("page_unload");
		});
		*/
		return typeof ret == "boolean" ? ret : false;
	};
});